import { defineStore } from "pinia";
import { tokens } from '@/comm/tokens'
export interface IToken {
    chainId: number;
    address: string;
    name: string;
    symbol: string;
    decimals: number;
    logoURI: string;
}

const URL_COIN: any = tokens;

interface IState {
    list: IToken[];
}
const mapImgs: any = {};
export const useTokens = defineStore("tokens", {
    state: (): IState => ({
        list: []
    }),

    getters: {},
    actions: {
        async init() {
            return;
            let url = "https://tokens.coingecko.com/uniswap/all.json";
            // let url = "https://alpha.chainingview.io/coins-all.json"
            fetch(url)
                .then(async res => {
                    let data = await res.json();
                    this.list = data.tokens;
                    localStorage.setItem("coins", JSON.stringify(this.list));
                })
                .catch(e => {
                    console.log("##jsz", e);
                    let str = localStorage.getItem("coins");
                    if (str) {
                        this.list = JSON.parse(str);
                    }
                });
        },
        getImg(val: string) {
            let str = val ? val.toLowerCase() : "";
            str = str.replace(/\s+$/, "");
            if (str != "usdt") {
                str = str.replace("usdt", "");
            }
            // return "https://alpha.chainingview.io/coins/" + str + ".png";
            if (location.origin.includes("chainingview")) {
                // console.log('找图片',location.origin + "/coins/" + str + ".png")
                return location.origin + "/coins/" + str + ".png";
                // return "https://alpha.chainingview.io/coins/" + str + ".png";
            }
            // return "http://192.168.150.188/coins/" + str + ".png";
            return '/coin.svg'
            // return "https://alpha.chainingview.io/coins/" + str + ".png";

            if (URL_COIN[str]) {
                return URL_COIN[str];
            }
            // str = str.replace(/\s+$/, "");
            // if (str == "eth" || str == "ethusdt") {
            //     return "/ethereum.webp";
            // }
            str = str.replace(/^100+/, "");

            // if (str == "usdt") {
            //     let t = this.list.find(i => i.symbol == "USDT");
            //     if (t) {
            //         return t.logoURI;
            //     }

            // }
            if (val === undefined) {
                return "/coin.svg";
            }
            let item: any = this.list.find(i => i.symbol.toLowerCase() == str.replace("usdt", ""));
            if (item) {
                return item.logoURI;
            }
            return "/coin.svg";
        },
        getImgByHash(val: string) {
            if (val == '0x') return 'https://assets.coingecko.com/coins/images/279/standard/ethereum.png?1696501628'
            if (mapImgs[val]) return mapImgs[val];
            let imgItem = "/coin.svg";
            let item = this.list.find(i => i.address == val);

            if (item) {
                // console.log("find coin", item)
                imgItem = item.logoURI;
            }
            // console.log("no find coin", val)
            mapImgs[val] = imgItem;
            return imgItem;
        }
    }
});
